package top.yxqz.session.demo.web;

import top.yxqz.session.demo.school.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

//view
@WebServlet("/sessionIndexServlet")
public class sessionIndexServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
//        得到session会话对象里面的存储的对象
        Student user = (Student) session.getAttribute("user");
        if (user == null) {
//            response.sendRedirect(getServletContext().getContextPath()+"/login");
            response.getWriter().write("您还未登录，请<a href=" + getServletContext().getContextPath() + "/login>登录</a>");
        } else {
            response.getWriter().write("您已登录，欢迎您" + user.getUserName());
            response.getWriter().write("<a href=" + getServletContext().getContextPath() + "/sessionLogoutServlet>退出</a>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
